package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.yuncheng.spcyApi.vo.spcy.SpcyXpryUserVo;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxXpjl;
import com.yuncheng.spcyApi.mapper.SpcySqsxXpjlMapper;
import com.yuncheng.spcyApi.service.ISpcySqsxXpjlService;
import com.yuncheng.spcyApi.utils.GetCurrentUserUtils;
import com.yuncheng.spcyApi.utils.OrderItemUtils;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.PageVo;
import com.yuncheng.spcyApi.vo.sqsx.SqsxZzZyYbNumVo;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 审评查验-选派记录
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcySqsxXpjlServiceImpl extends BaseServiceImpl<SpcySqsxXpjlMapper, SpcySqsxXpjl> implements ISpcySqsxXpjlService {

    @Resource
    private SpcySqsxXpjlMapper spcySqsxXpjlMapper;

    @Override
    public List<SpcySqsxXpjl> getXpjlBySqsxId(String sqsxid, String flowType) {
        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);
        if (StringUtils.isNotBlank(flowType)) {
            queryWrapper.eq(SpcySqsxXpjl::getFspcylx, flowType);
        }
        queryWrapper.orderByAsc(SpcySqsxXpjl::getFssk);
        queryWrapper.orderByDesc(SpcySqsxXpjl::getFzpsj);
        queryWrapper.orderByAsc(SpcySqsxXpjl::getFsftyxp);
        List<SpcySqsxXpjl> xpjlList = list(queryWrapper);
        return xpjlList;
    }

    @Override
    public Page<SpcySqsxXpjl> getXpjlPageListBySqsxId(String sqsxid, String key, Integer pageNo, Integer pageSize) {
        Page<SpcySqsxXpjl> page = new Page<>(pageNo, pageSize);

        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        if (StringUtils.isNotBlank(key)){
            queryWrapper.and(q-> q.like(SpcySqsxXpjl::getFjcyxm, key)
                    .or()
                    .like(SpcySqsxXpjl::getFgzdw, key)
                    .or()
                    .like(SpcySqsxXpjl::getFxpfs, key)
                    .or()
                    .like(SpcySqsxXpjl::getFsftyxp, key)
                    .or()
                    .like(SpcySqsxXpjl::getFssk, key)
            );
        }

        queryWrapper.last("order by instr ('组长,组员,预备检查员', fssk), instr ('同意,不同意', fsftyxp), create_time desc");

        return page(page, queryWrapper);
    }

    @Override
    public List<SpcySqsxXpjl> getTyXpXpjlList(String sqsxid, String flowType) {

        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        if (StringUtils.isBlank(flowType)){
            flowType = SpcyConstant.FLOW_TYPE_CY;
        }

        queryWrapper.eq(SpcySqsxXpjl::getFspcylx, flowType);
        queryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);

        /*queryWrapper.orderByAsc(SpcySqsxXpjl::getFssk);
        queryWrapper.orderByDesc(SpcySqsxXpjl::getFzpsj);
        queryWrapper.orderByAsc(SpcySqsxXpjl::getFsftyxp);*/
        queryWrapper.last("order by instr ('组长,组员,预备检查员', fssk), fzpsj desc");

        List<SpcySqsxXpjl> tyxpjlList = list(queryWrapper);
        return tyxpjlList;
    }

    @Override
    public List<SpcyXpryUserVo> getTyxpUserBeanListByFsqsxId(String sqsxid) {
        return spcySqsxXpjlMapper.selectXpUserBeanListByFsqsxId(sqsxid, SpcyConstant.XPJL_SFTYXP_YES);
    }

    @Override
    public List<SpcyXpryUserVo> getXpUserBeanListByFsqsxId(String sqsxid, String fsftyxp) {
        return spcySqsxXpjlMapper.selectXpUserBeanListByFsqsxId(sqsxid, fsftyxp);
    }

    @Override
    public List<SpcySqsxXpjl> getTyXpXpjlListNotOrder(String sqsxid) {
        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        queryWrapper.eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_CY);
        queryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);

        List<SpcySqsxXpjl> tyxpjlList = list(queryWrapper);
        return tyxpjlList;
    }

    @Override
    public List<String> getTyXpXpjlListNotSendJctzSms(String sqsxid) {
        /*LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);*/

        /*queryWrapper.and(q -> q
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES)
                .isNull(SpcySqsxXpjl::getFjctzdxsj)
                .or()
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_NO)
                .isNotNull(SpcySqsxXpjl::getFjctzdxsj)

        );*/
        /*queryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
        queryWrapper.isNull(SpcySqsxXpjl::getFjctzdxsj);*/

//        return list(queryWrapper);

        return spcySqsxXpjlMapper.findTyxpryBgList(sqsxid, SpcyConstant.XPJL_SFTYXP_YES, SpcyConstant.XPJL_SFTYXP_NO);
    }

    @Override
    public List<SpcySqsxXpjl> getDqrXpjlNotSendSmsztList(String sqsxid) {
        return list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_WQY)
                .eq(SpcySqsxXpjl::getFsmszt, SpcyConstant.XPJL_SMSZT_NO));
    }

    @Override
    public void updateXpjlInJctzSmsEmpty(String sqsxid) {

        spcySqsxXpjlMapper.updateXpjlInJctzSmsEmpty(sqsxid, SpcyConstant.XPJL_SFTYXP_NO);
    }

    @Override
    public void updateDqrXpjlToWcl(String sqsxid, String bz) {
        LambdaUpdateWrapper<SpcySqsxXpjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);
        updateWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_WQY);
        updateWrapper.eq(SpcySqsxXpjl::getFsmszt, SpcyConstant.XPJL_SMSZT_NO);

        SpcySqsxXpjl updateBean = new SpcySqsxXpjl();
        updateBean.setFsftyxp(SpcyConstant.XPJL_SFTYXP_WCL);
        updateBean.setFbz(bz);

        update(updateBean, updateWrapper);
    }

    @Override
    public void updateJctzSms(String id, String fjctzdxnr) {
        LambdaUpdateWrapper<SpcySqsxXpjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxXpjl::getId, id);

        SpcySqsxXpjl updateBean = new SpcySqsxXpjl();
        updateBean.setFjctzdxsj(new Date());
        updateBean.setFjctzdxzt("是");
        updateBean.setFjctzdxnr(fjctzdxnr);
        update(updateBean, updateWrapper);
    }

    @Override
    public List<SpcySqsxXpjl> getDqrXpXpjlList(String sqsxid, String flowType, boolean isOrder) {
        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        if (StringUtils.isBlank(flowType)){
            flowType = SpcyConstant.FLOW_TYPE_CY;
        }

        queryWrapper.eq(SpcySqsxXpjl::getFspcylx, flowType);
        queryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_WQY);

        if (isOrder) {
            queryWrapper.last("order by instr ('组长,组员,预备检查员', fssk), create_time desc");
        }

        List<SpcySqsxXpjl> tyxpjlList = list(queryWrapper);
        return tyxpjlList;
    }

    @Override
    public List<SpcySqsxXpjl> getTyXpXpjlZsjcyList(String sqsxid) {
        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);
        queryWrapper.eq(SpcySqsxXpjl::getFspcylx, SpcyConstant.FLOW_TYPE_CY);
        queryWrapper.eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_YES);
        queryWrapper.ne(SpcySqsxXpjl::getFssk, SpcyConstant.HCY_SSK_YB);

        queryWrapper.orderByAsc(SpcySqsxXpjl::getFssk);
        queryWrapper.orderByDesc(SpcySqsxXpjl::getFzpsj);
        List<SpcySqsxXpjl> xpjlList = list(queryWrapper);
        return xpjlList;
    }

    @Override
    public Map getJcyYearJcqk(String userid, String year) {

        int hjxpCount = 0; // 合计选派
        int hjjcCount = 0; // 合计检查
        int jjxpCount = 0; // 拒绝选派
        String cqlCount = "0%"; // 出勤率

        List<SpcySqsxXpjl> totalList = this.getYearJcyXpjlqkList(userid, year);

        hjxpCount = totalList.size();

        if (CollectionUtil.isNotEmpty(totalList)){
            hjjcCount = totalList.stream()
                    .filter(x -> StringUtils.isNotBlank(x.getFsftyxp())
                            && x.getFsftyxp().equals(SpcyConstant.XPJL_SFTYXP_YES)
                    )
                    .collect(Collectors.toList())
                    .size();

            jjxpCount = totalList.stream()
                    .filter(x -> StringUtils.isNotBlank(x.getFsftyxp())
                            && x.getFsftyxp().equals(SpcyConstant.XPJL_SFTYXP_NO)
                    )
                    .collect(Collectors.toList())
                    .size();
        }

        cqlCount = jsNdkpCql(hjxpCount, hjjcCount);

        Map resultMap = new HashMap();
        resultMap.put("hjxpCount", hjxpCount);
        resultMap.put("hjjcCount", hjjcCount);
        resultMap.put("jjxpCount", jjxpCount);
        resultMap.put("cqlCount", cqlCount);
        return resultMap;
    }

    @Override
    public String jsNdkpCql(int zxxpcs, int sjjccs) {
        String attendance = "0";
        // 中心选派次数 0
        if (zxxpcs == 0){
            attendance = "0";
        }else if (sjjccs == 0){
            attendance = "0";
        }else {
            NumberFormat numberFormat = NumberFormat.getInstance(); // 创建一个数值格式化对象
            numberFormat.setMaximumFractionDigits(2); // 设置精确到小数点后2位
            String format = numberFormat.format((float) sjjccs / (float) zxxpcs * 100); // 百分比
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(format)){
                attendance = format;
            }
        }
        return attendance;
    }

    @Override
    public void updateYbKpjgByJcy(String jcyid, String sqsxid, String kpjg) {
        /*List<SpcySqsxXpjl> list = list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFjcyid, jcyid)
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid));
        if (CollectionUtil.isNotEmpty(list)){
            for (SpcySqsxXpjl xpjl : list){
                xpjl.setFzhpj(kpjg);
                updateById(xpjl);
            }
        }*/

        LambdaUpdateWrapper<SpcySqsxXpjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxXpjl::getFjcyid, jcyid);
        updateWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        SpcySqsxXpjl updateBean = new SpcySqsxXpjl();
        updateBean.setFzhpj(kpjg);

        update(updateBean, updateWrapper);
    }

    @Override
    public Page<SpcySqsxXpjl> selectPageList(SpcySqsxXpjl query, PageVo pageVo, OrderItemVo orderItemVo, boolean onlyCurrentUser) {
        Page<SpcySqsxXpjl> page = new Page<>(pageVo.getPageNo(), pageVo.getPageSize());
        LambdaQueryWrapper<SpcySqsxXpjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFbjcqy()), SpcySqsxXpjl::getFbjcqy, query.getFbjcqy());
        queryWrapper.like(StringUtils.isNotBlank(query.getFzhpj()), SpcySqsxXpjl::getFzhpj, query.getFzhpj());
        queryWrapper.like(StringUtils.isNotBlank(query.getFssk()), SpcySqsxXpjl::getFssk, query.getFssk());
        queryWrapper.like(StringUtils.isNotBlank(query.getFsftyxp()), SpcySqsxXpjl::getFsftyxp, query.getFsftyxp());

        /** 排除合并检查 */
        queryWrapper.eq(SpcySqsxXpjl::getFhbjczt, 0);

        if (onlyCurrentUser){
            queryWrapper.eq(SpcySqsxXpjl::getFuserid, GetCurrentUserUtils.getCurrentUserId());
        }else {
            queryWrapper.eq(StringUtils.isNotBlank(query.getFuserid()), SpcySqsxXpjl::getFuserid, query.getFuserid());
        }
        if (StringUtils.isBlank(orderItemVo.getOrderItem())){
            queryWrapper.orderByDesc(SpcySqsxXpjl::getFzpsj);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        return page(page, queryWrapper);
    }

    @Override
    public List<SpcySqsxXpjl> getSqsxXpZzList(String sqsxid) {
        return list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .like(SpcySqsxXpjl::getFssk, SpcyConstant.HCY_SSK_ZZ));
    }

    @Override
    public List<SpcySqsxXpjl> getWqrXpXpjlList(String sqsxid) {
        return list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFsqsxid, sqsxid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_WQY));
    }

    @Override
    public void syncUpdateXpjlGzsjBySqsx(String sqsxid, Date gzkssj, Date gzjssj) {
        if (StringUtils.isBlank(sqsxid)){
            return;
        }

        LambdaUpdateWrapper<SpcySqsxXpjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxXpjl::getFsqsxid, sqsxid);

        SpcySqsxXpjl updateBean = new SpcySqsxXpjl();
        if (gzkssj != null) {
            updateBean.setFgzkssj(gzkssj);
        }

        if (gzjssj != null) {
            updateBean.setFgzjssj(gzjssj);
        }

        update(updateBean, updateWrapper);
    }

    @Override
    public List<SpcySqsxXpjl> getYearJcyXpjlqkList(String userid, String year) {
        return spcySqsxXpjlMapper.getYearXpjlqkList(userid, SpcyConstant.FLOW_TYPE_CY, year);
    }

    @Override
    public String getXpjlSqsxidById(String id) {
        return spcySqsxXpjlMapper.getXpjlSqsxidById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchInsertXpjlList(List<SpcySqsxXpjl> batchList) {
        if (CollectionUtil.isEmpty(batchList)){
            return;
        }

        List<List<SpcySqsxXpjl>> partitionWjList = ListUtils.partition(batchList, 30);
        partitionWjList.forEach(item -> {
            saveBatch(item);
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchAddXpjlList(List<SpcySqsxXpjl> batchList) {
        if (CollectionUtil.isEmpty(batchList)){
            return;
        }

        saveBatch(batchList);
    }

    @Override
    public List<SpcySqsxXpjl> getDsxpIndexWqrList(String fdsxpywid) {
        return list(new LambdaQueryWrapper<SpcySqsxXpjl>()
                .eq(SpcySqsxXpjl::getFdsxpywid, fdsxpywid)
                .eq(SpcySqsxXpjl::getFsftyxp, SpcyConstant.XPJL_SFTYXP_WQY));
    }

    @Override
    public List<SpcySqsxXpjl> getTyWqrFsdqrSmsList(String sqsxid) {
        return spcySqsxXpjlMapper.getTyWqrFsdqrSmsList(sqsxid, SpcyConstant.XPJL_SFTYXP_YES, SpcyConstant.XPJL_SFTYXP_WQY, SpcyConstant.XPJL_SMSZT_YES);
    }

    @Override
    public List<SpcySqsxXpjl> getXpListSizeByFsftyxp(String sqsxid, String fsftyxp) {
        return spcySqsxXpjlMapper.getXpListSizeByFsftyxp(sqsxid, fsftyxp);
    }

    @Override
    public List<String> getXpjlListSize(String sqsxid, String fsftyxp, String fsmszt) {
        return spcySqsxXpjlMapper.getXpjlListSize(sqsxid, fsftyxp, fsmszt);
    }

    @Override
    public SqsxZzZyYbNumVo getXpWqrFsdqrSmsZzZyYbNumBean(String sqsxid) {
        SqsxZzZyYbNumVo resultNum = new SqsxZzZyYbNumVo();
        List<SpcySqsxXpjl> list = this.getTyWqrFsdqrSmsList(sqsxid);
        if (CollectionUtil.isNotEmpty(list)){
            resultNum.setFzzrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)).collect(Collectors.toList()).size());
            resultNum.setFzyrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_ZY)).collect(Collectors.toList()).size());
            resultNum.setFybrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_YB)).collect(Collectors.toList()).size());
        }

        return resultNum;
    }

    @Override
    public SqsxZzZyYbNumVo getTyXpZzZyYbNumBean(String sqsxid) {
        SqsxZzZyYbNumVo resultNum = new SqsxZzZyYbNumVo();
        List<SpcySqsxXpjl> list = spcySqsxXpjlMapper.getXpListSizeByFsftyxp(sqsxid, SpcyConstant.XPJL_SFTYXP_YES);
        if (CollectionUtil.isNotEmpty(list)){
            resultNum.setFzzrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_ZZ)).collect(Collectors.toList()).size());
            resultNum.setFzyrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_ZY)).collect(Collectors.toList()).size());
            resultNum.setFybrs(list.stream().filter(f -> f.getFssk().equals(SpcyConstant.HCY_SSK_YB)).collect(Collectors.toList()).size());
        }
        return resultNum;
    }

    @Override
    public List<String> selectIdByFsqsxidAndFuserid(String sqsxid, String userid) {
        return spcySqsxXpjlMapper.selectIdByFsqsxidAndFuserid(sqsxid, userid);
    }

    @Override
    public List<String> selectFsftyxpIdByFsqsxidAndFuserid(String sqsxid, String userid, String fsftyxp) {
        return spcySqsxXpjlMapper.selectFsftyxpIdByFsqsxidAndFuserid(sqsxid, userid, fsftyxp);
    }

    @Override
    public Integer selectTySskSize(String sqsxid, String fsftyxp, String fssk) {
        return spcySqsxXpjlMapper.selectTySskSize(sqsxid, fsftyxp, fssk);
    }

}
